package com.woniuxy.qiantai.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.woniuxy.qiantai.entity.Shop;
import com.woniuxy.qiantai.entity.Type;
import com.woniuxy.qiantai.service.ShopAllService;
import com.woniuxy.qiantai.service.ShopService;
import com.woniuxy.qiantai.service.TypeService;
import com.woniuxy.qiantai.vo.ShopAll;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author firstGroup
 * @since 2023-02-22 09:45:58
 */
@Controller
@RequestMapping("/type")
public class TypeController {
    @Autowired
    TypeService typeService;
    @Autowired
    ShopService shopService;
    @Autowired
    ShopAllService shopAllService;

    @RequestMapping("types")
    public String getTypes() {
        return "types";
    }

    @RequestMapping("typeInfo")
    @ResponseBody
    public List<Type> getShopType(Integer shopId) { //用于获取从后台查询到的商家类型标签信息
        List<Type> typeInfo = typeService.selectTypeByShopId(shopId);
        return typeInfo;
    }

    /**
     * 用于查询食物种类
     *
     * @return List<Map < String, Object>>即[{type_name: "奶茶"}, {type_name: "饮料"}]
     */
    @RequestMapping("all")
    @ResponseBody
    public List<Map<String, Object>> getTypeAll() {
        QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("type_name");
        queryWrapper.groupBy("type_name");
        List<Map<String, Object>> maps = typeService.listMaps(queryWrapper);
        return maps;
    }

    @RequestMapping("toSelectShopByType")
    public String toSelectShopByTypeQuery(String typeName,Model model) {
        model.addAttribute("typeName",typeName);
        return "selectShopByType";
    }
    @RequestMapping("selectShopAllByType")
    @ResponseBody
    public PageInfo<ShopAll> selectShopAllByTypeQuery(String typeName, Integer currentPage,
                                                      Integer pageSize) {

        if (currentPage == null || currentPage <= 0) currentPage = 1;
        if (pageSize == null || pageSize <= 0) pageSize = 3;
        return shopAllService.selectShopAllByType(typeName, currentPage, pageSize);
    }

}

